/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service;

import com.openbrew.openbrewrf.Administration;
import com.openbrew.openbrewrf.FinishedGoodsInventory;
import com.openbrew.openbrewrf.views.FGIView;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import service.miscListModels.FGIList;
import tools.FGITools;
import tools.InventoryModifiers;

/**
 *
 * @author chapmand
 */
@Stateless
@Path("com.openbrew.openbrewrf.finishedgoodsinventory")
public class FinishedGoodsInventoryFacadeREST extends AbstractFacade<FinishedGoodsInventory> {
    @PersistenceContext(unitName = "com.openBrew_openBrewRF_war_1.0PU")
    private EntityManager em;

    public FinishedGoodsInventoryFacadeREST() {
        super(FinishedGoodsInventory.class);
    }

    @POST
    @Override
    @WebMethod(operationName = "create")
    @Consumes({"application/xml", "application/json"})
    public FinishedGoodsInventory create(@WebParam(name = "finishedgoodsinventory") FinishedGoodsInventory entity) {
        entity = super.create(entity);
        getEntityManager().getEntityManagerFactory().getCache().evict(FGIView.class);
        return entity;
    }

    @POST
    @Path("updateAllFGIs")
    @Consumes({"application/xml", "application/json"})
    public void updateFGIList(List<FinishedGoodsInventory> entities) {
        if(entities != null){
            for(FinishedGoodsInventory entity : entities){
                super.edit(entity);
            }
        }
    }

    @PUT
    @Override
    @Consumes({"application/xml", "application/json"})
    public void edit(FinishedGoodsInventory entity) {
        super.edit(entity);
        getEntityManager().getEntityManagerFactory().getCache().evict(FGIView.class);
    }

    @PUT
    @Path("processFGIBottleAddition/{fgi}/{qty}/{batch}/{employee}/{date}")
    @Consumes({"application/xml", "application/json"})
    public void editFGIBottleAddition(@PathParam("fgi") int fgiid,
            @PathParam("batch") int brewid, @PathParam("qty") int qty,
            @PathParam("employee") int employeeID, @PathParam("date") Date date) {
        if(fgiid != 0){
            final FGITools fgit = new FGITools();
            fgit.doFGIBottleAddition(em, fgiid, qty, brewid, employeeID, date);
            getEntityManager().flush();
        }
        getEntityManager().getEntityManagerFactory().getCache().evict(FGIView.class);
    }

    @PUT
    @Path("processFGIKegAddition/{fgi}/{qty}/{batch}/{employee}/{date}")
    @Consumes({"application/xml", "application/json"})
    public void editFGIKegAddition(@PathParam("fgi") int fgiid,
            @PathParam("batch") int brewid, @PathParam("qty") int qty,
            @PathParam("employee") int employeeID, @PathParam("date") Date date) {
        if(fgiid != 0){
            final FGITools fgit = new FGITools();
            fgit.doFGIKegAddition(em, fgiid, qty, brewid, employeeID, date);
            getEntityManager().flush();
        }
        getEntityManager().getEntityManagerFactory().getCache().evict(FGIView.class);
    }

    @PUT
    @Path("processFGIServingVesselAddition/{fgi}/{qty}/{batch}/{employee}/{date}")
    @Consumes({"application/xml", "application/json"})
    public void editFGIServingVesselAddition(@PathParam("fgi") int fgiid,
            @PathParam("batch") int brewid, @PathParam("qty") float qty,
            @PathParam("employee") int employeeID, @PathParam("date") Date date) {
        if(fgiid != 0){
            final FGITools fgit = new FGITools();
            fgit.doFGIServingVesselAddition(em, fgiid, qty, brewid, employeeID, date);
            getEntityManager().flush();
        }
        getEntityManager().getEntityManagerFactory().getCache().evict(FGIView.class);
    }

    @PUT
    @Path("processFGIKegSplit/{fgiOld}/{qtyOld}/{fgiNew}/{qtyNew}")
    @Consumes({"application/xml", "application/json"})
    public void editFGIKegSplit(@PathParam("fgiOld") int oldFGIid,
            @PathParam("qtyOld") int oldQty, @PathParam("fgiNew") int newFGIid,
            @PathParam("qtyNew") int newQty) {
        if((oldFGIid != 0)&&(newFGIid != 0)){
            final FGITools fgit = new FGITools();
            fgit.doFGIKegSplit(em, oldFGIid, oldQty, newFGIid, newQty);
        }
        getEntityManager().getEntityManagerFactory().getCache().evict(FGIView.class);
    }

    @PUT
    @Path("processFGIServingVesselSplit/{fgiOld}/{fgiNew}/{qtyNew}")
    @Consumes({"application/xml", "application/json"})
    public void editFGIServingVesselSplit(@PathParam("fgiOld") int oldFGIid
            , @PathParam("fgiNew") int newFGIid, @PathParam("qtyNew") int newQty) {
        if((oldFGIid != 0)&&(newFGIid != 0)){
            final FGITools fgit = new FGITools();
            fgit.doFGIVesselSplit(em, oldFGIid, newFGIid, newQty);
        } 
        getEntityManager().getEntityManagerFactory().getCache().evict(FGIView.class);
    }

    @PUT
    @Path("processBarrelFGIAddition/{fgi}/{qty}/{batch}/{employee}")
    @Consumes({"application/xml", "application/json"})
    public void editFGIBarrelAddition(@PathParam("fgi") int fgiid,
            @PathParam("batch") int bcsid, @PathParam("qty") int qty,
            @PathParam("employee") int employeeID) {
        if(fgiid != 0){
            final FGITools fgit = new FGITools();
            fgit.doFGIBarreledAddition(em, fgiid, qty, bcsid, employeeID);
            getEntityManager().flush();
        }
        getEntityManager().getEntityManagerFactory().getCache().evict(FGIView.class);
    }

    @DELETE
    @Path("{id}")
    public void remove(@PathParam("id") Integer id) {
        super.remove(super.find(id));
        getEntityManager().getEntityManagerFactory().getCache().evict(FGIView.class);
    }

    @GET
    @Path("{id}")
    @Produces({"application/xml", "application/json"})
    public FinishedGoodsInventory find(@PathParam("id") Integer id) {
        return super.find(id);
    }

    @GET
    @Override
    @Produces({"application/xml", "application/json"})
    public List<FinishedGoodsInventory> findAll() {
        return super.findAll();
    }

    @GET
    @Path("runFGIAllocationFix")
    @Produces({"application/xml", "application/json"})
    public List<FinishedGoodsInventory> runFGIAllocationFix() {
        InventoryModifiers im = new InventoryModifiers();
        im.runFGIAllocationFix(em);
        getEntityManager().getEntityManagerFactory().getCache().evict(FGIView.class);
        return em.createNamedQuery("FinishedGoodsInventory.findAll").getResultList();
    }

    @GET
    @Path("{from}/{to}")
    @Produces({"application/xml", "application/json"})
    public List<FinishedGoodsInventory> findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) {
        return super.findRange(new int[]{from, to});
    }
    
    @GET
    @Path("runNamedQuery/{str}")
    @Override
    @Produces({"application/xml", "application/json"})
    public List<FinishedGoodsInventory> runNamedQuery(@PathParam("str") String str){
        return super.runNamedQuery(str);
    }

    @GET
    @Path("count")
    @Produces("text/plain")
    public String countREST() {
        return String.valueOf(super.count());
    }

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
    
    @GET
    @Path("runQuery/{str}")
    @Override
    @Produces({"application/xml", "application/json"})
    public List<FinishedGoodsInventory> runQuery(@PathParam("str") String str){
        return super.runQuery(str);
    }
    
    @GET
    @Path("getPackageableFGIs/{id}")
    @Produces({"application/xml", "application/json"})
    public List<FinishedGoodsInventory> findPackageableFGIs(@PathParam("id") Integer beerID){
        List<FinishedGoodsInventory> fgis = super.findAll();
        List<FinishedGoodsInventory> pFGIs = new ArrayList<FinishedGoodsInventory>();
        final Administration admin = getAdmin(em);
        for(final FinishedGoodsInventory fgi : fgis){
            if(fgi.getFGIBeerID()!=null){
                if(fgi.getFGIBeerID().getbId() == (int)beerID){
                    if(fgi.getFGIPackageID()!=null){
                        if(admin.getEnableScan()){
                            if(fgi.getFGIPackageID().getPackageTypeVol()<= 0.0322){
                                pFGIs.add(fgi);
                            }
                        } else {
                            pFGIs.add(fgi);
                        }
                    }
                } 
            }
        }
        return pFGIs;
    }

    /*************************************************************************
     * Database access files.
     *************************************************************************/
    private Administration getAdmin(final EntityManager en) {
        TypedQuery<Administration> query = en.createQuery(
                "SELECT a FROM Administration a WHERE a.aNum = '1'", Administration.class);
        return query.getSingleResult();
    }

    @POST
    @Path("insertFGIList")
    @Consumes({"application/xml", "application/json"})
    public FGIList insertFGIList(FGIList entity) {
        List<FinishedGoodsInventory> fgis = new ArrayList<FinishedGoodsInventory>();
        if(entity.getFgis() != null){
            for(FinishedGoodsInventory fgi : entity.getFgis()){
                fgis.add(super.create(fgi));
            }
            getEntityManager().getEntityManagerFactory().getCache().evict(FGIView.class);
        }
        FGIList list = new FGIList();
        list.setFgis(fgis);
        return list;
    }
}
